import {get} from "@utils/http"
import dayjs from "dayjs"
// pages/mypost/mypost.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentIndex:0,
    dynamicList:[],
    commemts:[],
    myComments:[],
  },
  loveDynamic(e){
    const dynamicList=this.data.dynamicList
    const index=e.currentTarget.dataset.index
    if(dynamicList[index].islove){
      dynamicList[index].lovenumber--
    }else{
      dynamicList[index].lovenumber++
    }
    dynamicList[index].islove=!dynamicList[index].islove
    this.setData({
      dynamicList
    })
  },
  async getdynamic(){
    var res=await get("/dynamics",{
      personal:true
    })
    var dynamicslist=[]
    for (let i = 0; i < res.content.length; i++) {
      const item = res.content[i];
      const dynamics={
        id:item[0],
        content:item[1],
        avatar:item[11],
        anonymous:item[4],
        provided:item[5],
        contact:item[9],
        name:item[10],
        commentnumber:item[13],
        lovenumber:item[6],
        sex:item[12],
        islove:item[14],
        userid:item[8],
        tags:[],
        srcs:[],
        diffTime:""
      }
      const tags=item[3].split(",")
      dynamics.tags=tags
    dynamics.srcs=item[2].split(",")
    const createTime=dayjs(item[7])
    if(dayjs().diff(createTime,"year")){
      dynamics.diffTime=dayjs().diff(createTime,"year")+"年前"
    }else{
      if (dayjs().diff(createTime,"month")){
        dynamics.diffTime=dayjs().diff(createTime,"month")+"月前"
      }else{
        if(dayjs().diff(createTime,"day")){
          dynamics.diffTime=dayjs().diff(createTime,"day")+"日前"
        }else{
          if(dayjs().diff(createTime,"hour")){
            dynamics.diffTime=dayjs().diff(createTime,"hour")+"小时前"
          }else{
            if(dayjs().diff(createTime,"m")){
              dynamics.diffTime=dayjs().diff(createTime,"m")+'分钟前'
            }else{
              dynamics.diffTime="刚刚"
            }
          }
        }
      }
    }
      dynamicslist.push(dynamics)
    }
    this.setData({
      dynamicList:dynamicslist
    })
  },
  async getcomment(){
    var res=await get('/dynamics/comment/receive')
    this.setData({
      comments:res.map(item=>{
        const ts=item.updatedAt
        item.time=dayjs(ts).format("YYYY/MM/DD")
        item.name=item.userName
        return item
      })
    })
  },
  async getmycomment(){
    var res=await get('/dynamics/comment/myself')
    this.setData({
      myComments:res.map(item=>{
        const ts=item.updatedAt
        item.time=dayjs(ts).format("YYYY/MM/DD")
        item.name=item.userName
        return item
      })
    })
  },
  changeIndex(e){
    this.setData({
      currentIndex:e.detail.index
    })
    this.getDetail()
  },
  getDetail(){
    switch(this.data.currentIndex){
      case 0:
        this.getdynamic()
        break
      case 1:
        this.getcomment()
        break
      case 2:
        this.getmycomment()
        break
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getDetail()
  },
 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})